package com.xiaoshuidi.cloud.module.iot.dal.util;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.xiaoshuidi.cloud.module.iot.dal.model.callback.SendMessageVo;
import com.xiaoshuidi.cloud.module.iot.dal.pojo.TbDeviceDo;
import com.xiaoshuidi.cloud.module.rooms.vo.PriceInfo;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.springframework.beans.factory.annotation.Value;

import java.math.BigDecimal;
import java.util.List;

/**
 * @BelongsProject: lease-module-new-iot
 * @BelongsPackage: com.xiaoshuidi.cloud.module.iot.uitils
 * @Author: James
 * @CreateTime: 2023-06-30  14:27
 * @Description: TODO
 * @Version: 1.0
 */
public class SendMqMessageUtil {
    public static BigDecimal compute(BigDecimal sourceTotal, BigDecimal currentTotal, List<PriceInfo> list) {
        if(CollectionUtil.isEmpty(list)){
            return new BigDecimal(0);
        }
        BigDecimal diff = currentTotal.subtract(sourceTotal);
        BigDecimal totalAmount = BigDecimal.ZERO;
        for (int i = 0; i < list.size(); i++) {
            PriceInfo s = list.get(i);
            if (currentTotal.compareTo(s.getRead()) > 0) {
                BigDecimal currentDiff = currentTotal.subtract(s.getRead());
                if (currentDiff.compareTo(diff) < 0) {
                    BigDecimal amount = currentDiff.multiply(s.getPrice());
                    totalAmount = totalAmount.add(amount);
                    amount = compute(sourceTotal, s.getRead(), list);
                    totalAmount = totalAmount.add(amount);
                    break;
                } else {
                    BigDecimal amount = diff.multiply(s.getPrice());
                    totalAmount = totalAmount.add(amount);
                    break;
                }
            }
        }
        return totalAmount;
    }
}
